﻿using B.S.BasicData.Api.Read.Application.Command.User;
using B.S.BasicData.Api.Read.DTOS;
using B.S.BasicData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace B.S.BasicData.Api.Read.Controllers
{
    [Route("api/[controller][action]")]
    [ApiController]
    [Authorize]
    public class UserController : ControllerBase
    {
        private readonly IMediator _mediator;    
        private readonly ILogger<UserController> logger;

        public UserController(IMediator mediator, ILogger<UserController> logger)
        {
            _mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 获取省份下拉框
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<ApiResult<List<ProvinceDto>>> GetProvince([FromQuery]GetProvinceCommand request)
        {
            try
            {
                logger.LogInformation("获取省份成功");
                return _mediator.Send(request);
            }
            catch (Exception ex)
            {

                logger.LogError("获取省份失败" + ex.Message);
                throw ex;
            }
        }
        /// <summary>
        /// 查询城市
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<ApiResult<List<CityDto>>> GetCity([FromQuery] GetCityCommand request)
        {
            try
            {
                logger.LogInformation("获取城市数据成功");
                return _mediator.Send(request);
            }
            catch (Exception ex)
            {

                logger.LogError("获取失败" + ex.Message);
                throw ex;
            }
        }
        /// <summary>
        /// 获取县级下拉框
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<ApiResult<List<CountyDto>>> GetCounty([FromQuery]GetCountyCommand request)
        {
            try
            {
                logger.LogInformation("获取县级数据成功"); ;
                return _mediator.Send(request);
            }
            catch (Exception ex)
            {

                logger.LogError("获取失败" + ex.Message);
                throw ex;
            }
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<ApiPaging<UserDto>> GetUserList([FromQuery]GetUserCommand request)
        {
            try
            {
                logger.LogInformation("获取用户数据成功");
                return _mediator.Send(request);
            }
            catch (Exception ex)
            {

                logger.LogError("获取失败" + ex.Message);
                throw ex;
            }
        }
        /// <summary>
        /// 获取角色列表数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<ApiResult<List<RoleDto>>> GetRoleList([FromQuery]GetRoleListCommand request)
        {
            //Jwt取值
            var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            var userName = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
            var roleName = HttpContext.User.FindFirst(ClaimTypes.Role)?.Value;
            var customerValue = HttpContext.User.FindFirst("CustomClaimType")?.Value;
            var key = HttpContext.User.FindFirst("Key")?.Value;

            return _mediator.Send(request);
        }
        /// <summary>
        /// 根据部门Id获取员工信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<ApiResult<List<UserEmpDto>>> GetUserEmp([FromQuery]GetUserEmpListCommand request)
        {
            return _mediator.Send(request);
        }
        /// <summary>
        ///获取用户角色
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<ApiResult<List<UserRoleDto>>> GetRoleUsers([FromQuery]GetUserRoleCommand request)
        {
            return _mediator.Send(request);
        }
    }
}
